<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Http\Request;
use Throwable;

/**
 * 自定义业务接口异常处理类
 *
 * @package App\Exceptions
 */
class BusinessException extends Exception
{
    private $errno;
    private $errmsg;
    public function __construct(array $responseCode, array $extend = [], Throwable $previous = null)
    {
        list($code, $message) = $responseCode;
        $this->errno =$code;
        $this->errmsg = $message;
        // 附加的信息
        if(!empty($extend)) {
            $field = array_keys($extend)[0];
            $this->errmsg = str_replace( '%' . $field, ',' . $extend[$field], $this->errmsg);
        } else {
            $this->errmsg = preg_replace("/%[a-z]+/", '', $this->errmsg);
        }
        parent::__construct($message, $code, $previous);
    }

    public function render()
    {
        return response()->json(['error_code' => $this->errno, 'error_msg' => $this->errmsg]);
    }
}
